const mysql = require('mysql');


module.exports = (sql,params) => {
    //1.创建链接对象
    let connection = mysql.createConnection({
        host:'localhost',
        user:'root',
        password:'root',
        database:'blog_1810'
    })

    //2.开启链接
    connection.connect();

    //3.和数据库交互的过程是异步的

    /**
     * pending  进行中
     * 
     * fulfilled 已成功
     * 
     * rejected  已失败
     * 
     * pending--->fulfilled  resolve
     * 
     * pending --->rejected  reject
     * 
     * promise的特点：1.内部的状态改变不会收外界影响  2.状态一旦发生改变就不会在变
    */

    return new Promise((resolve,reject) => {
        //3.交互
        connection.query(sql,params,(error,result) => {
            if(error){
                reject(error)
            }else{
                resolve(result)
            }
            //关闭链接
            connection.end();
        })
    })
}